'use strict';
const db = uniCloud.database();
const type_list = db.collection('type_list');
const integral = db.collection('integral');
let uniID = require('uni-id')

exports.main = async (event, context) => {
	uniID = uniID.createInstance({
		context
	})
	let params = event.params || {}
	let payload = {}
	var data_eve = {}
	if (!event.uniIdToken) {
		return {
			code: 403,
			msg: '缺少token'
		}
	}
	payload = await uniID.checkToken(event.uniIdToken, {
		needPermission: true
	})
	if (payload.code && payload.code > 0) {
		return payload
	}
	params.uid = payload.uid
	const {
		userInfo,
	} = await uniID.checkToken(event.uniIdToken, {
		needPermission: true
	})




	let res = {}
	const {
		id
	} = event
	const {
		data
	} = await type_list.where({
		id
	}).get()
	const {
		uid,
	} = params
	const integral_A = await integral.add({
		uid,
		name: id == 1 ? '分享微信好友' : '分享朋友圈',
		color: 'blue--dark',
		integral: '+' + String(data[0].number),
		time: new Date().getTime()
	})
	res = await uniID.updateUser({
		uid,
		number: Number(userInfo.number) + Number(data[0].number),
	});

	return {
		code: 0,
		msg: "分享成功"
	}
};
